package com.gdth.bank.utils;

import android.annotation.TargetApi;
import android.app.ActivityManager;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import com.gdth.bank.R;

import com.gdth.database.bean.NotificationT;
import com.gdth.service.NotificationBroadcase;

import java.util.List;

/**
 * Created by xjc on 2018\5\15 0015.
 */


public class NotificationUtil extends ContextWrapper {

    private NotificationManager manager;
    public static final String id = "channel_1";
    public static final String name = "channel_name_1";

    public NotificationUtil(Context context){
        super(context);
    }

    @TargetApi(26)
    public void createNotificationChannel(){
        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
        getManager().createNotificationChannel(channel);
    }
    private NotificationManager getManager(){
        if (manager == null){
            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        }
        return manager;
    }
    @TargetApi(26)
    public Notification.Builder getChannelNotification(final Context context, NotificationT data){
    	if(TextUtils.isEmpty(data.getTzlj())) {
    		  return new Notification.Builder(getApplicationContext(), id)
            		  .setContentTitle(data.getTzbt())
                      .setContentText(data.getTznr())
                    .setSmallIcon(R.drawable.bank_logo)
                    .setAutoCancel(true);
    	}else {
    		 Intent intent =new Intent (context,NotificationBroadcase.class);
             intent.putExtra("path",data.getTzlj());
             intent.putExtra("name",data.getTzcs());
             intent.putExtra("xxid",String.valueOf(data.getXxtsId()));
             PendingIntent pendingIntent = PendingIntent.getBroadcast(context,  Integer.parseInt(String.valueOf(data.getXxtsId())), intent, PendingIntent.FLAG_UPDATE_CURRENT);

             return new Notification.Builder(getApplicationContext(), id)
           		  .setContentTitle(data.getTzbt())
                     .setContentText(data.getTznr())
                   .setSmallIcon(R.drawable.bank_logo)
                   .setAutoCancel(true).setContentIntent(pendingIntent);
    	}
    }
    public NotificationCompat.Builder getNotification_25(final Context context,NotificationT data){
    	if(TextUtils.isEmpty(data.getTzlj())) {
	        return new NotificationCompat.Builder(getApplicationContext())
	                .setContentTitle(data.getTzbt())
	                .setContentText(data.getTznr())
	                .setSmallIcon(R.drawable.bank_logo)
	                .setAutoCancel(true);
    	}else {
    		  Intent intent =new Intent (context,NotificationBroadcase.class);
              intent.putExtra("path",data.getTzlj());
              intent.putExtra("name",data.getTzcs());
              intent.putExtra("xxid",String.valueOf(data.getXxtsId()));
              PendingIntent pendingIntent = PendingIntent.getBroadcast(context,  Integer.parseInt(String.valueOf(data.getXxtsId())), intent, PendingIntent.FLAG_UPDATE_CURRENT);

              return new NotificationCompat.Builder(getApplicationContext())
  	                .setContentTitle(data.getTzbt())
  	                .setContentText(data.getTznr())
  	                .setSmallIcon(R.drawable.bank_logo)
  	                .setAutoCancel(true)
  	                .setContentIntent(pendingIntent);
    	}
    }
    public void sendNotification(final Context context,NotificationT data){
        if(isApplicationBroughtToBackground(context)){
            if (Build.VERSION.SDK_INT>=26){
                createNotificationChannel();
                Notification notification = getChannelNotification
                        (context,data).build();
                getManager().notify(data.getXxtsId(),notification);
            }else{
                Notification notification = getNotification_25(context,data).build();
                getManager().notify(data.getXxtsId(),notification);
            }
        }
    }

    /**
     *判断当前应用程序处于前台还是后台
     * true -- 在后台 ， false -- 在前台
     */
    public static boolean isApplicationBroughtToBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }
}
